<div class="login-wrap">
	<form class="gd-form" id="signForm">
		<div class="title">Sign up</div>
		<div class="gd-form-item">
			<input class="gd-input" type="text" id="signEmail" placeholder="Email">
		</div>
		<div class="gd-form-item">
			<div class="gd-input-group">
				<input class="gd-input" type="text" id="signVerCode" placeholder="Verification code" maxlength="6" oninput="if(value.length>6)value=value.substring(0,6)">
				<div class="gd-input__append">
					<button class="gd-btn btn-info" type="button" onclick="sendSignCode(this)">Aquire</button>
				</div>
			</div>
		</div>
		<div class="gd-form-item">
			<input class="gd-input" type="password" id="signPwd" placeholder="Password(min 6 characters)" maxlength="20" oninput="if(value.length>20)value=value.substring(0,20)">
		</div>
		<div class="gd-form-item">
			<div class="tips">
				By signing up, you agree with Gugee Data's
				<a href="${contextPath}/policy">Terms of Services</a>
				and
				<a href="${contextPath}/policy?type=1">Privacy Policy</a>
			</div>
		</div>
		<div class="gd-form-item">
			<button type="button" class="gd-btn btn-info" onclick="signSubmit()">Submit</button>
		</div>
	</form>
</div>
<script>
	function sendSignCode(btn) {
	    let email = document.getElementById('signEmail').value
        if(!email || !checkEmail(email)) {
            showToast('please check Email')
            return false
        }
        checkExistEmail(email,btn)
	}
	function checkExistEmail(email,btn) {
        btn.showLoading()
	    _axios.post('/v1/validate/email/register', {
            email: email
	    }).then(res => {
	        if(res.Code === 200) {
                sendCode(email,btn)
	        } else {
                btn.hideLoading()
				showToast(res.Msg)
	        }
	    })
	}
    function signSubmit() {
        let code = document.getElementById('signVerCode').value
        let email = document.getElementById('signEmail').value
        let pwd = document.getElementById('signPwd').value
        if(!code){
            showToast('please check Verification code')
            return false
        }
        if(!email || !checkEmail(email)) {
            showToast('please check Email')
            return false
        }
        if(!pwd || pwd.length < 6) {
            showToast('please check Password')
            return false
        }
        showLoading()
        const chl = sessionStorage.getItem('chl')
        let url = '/v1/register'
        if(chl) {
            url += '?chl=' + chl
        }
        _axios.post(url, {
            code:code,
            email: email,
            password: pwd
        }).then(res => {
            if(res.Code === 200){
				login(email,pwd)
            } else {
                hideLoading()
            }
        })
    }
</script>